﻿using Microsoft.Xna.Framework.Input;

namespace BlockeyEssentials.Input
{
    public class KeyInput
    {
        public KeyInput()
        {
            _PrevState = Keyboard.GetState();
            _CurrState = Keyboard.GetState();
        }

        #region Methods

        /// <summary>
        /// Updates the Current and Previous State. 
        /// Must be called every frame
        /// </summary>
        public void Update()
        {
            _PrevState = _CurrState;
            _CurrState = Keyboard.GetState();
        }

        /// <summary>
        /// Determines if a key has been pressed the moment called
        /// </summary>
        /// <param name="oKeys">Keys to Check</param>
        /// <returns>True if a key was just pressed</returns>
        public bool JustPressed(params Keys[] oKeys)
        {
            foreach (Keys key in oKeys)
            {
                bool keyUp = _PrevState.IsKeyUp(key);
                bool keyDown = _CurrState.IsKeyDown(key);

                if (keyUp && keyDown)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Determines if a key has been released the moment called
        /// </summary>
        /// <param name="oKeys">Keys to Check</param>
        /// <returns>True if a key was just released</returns>
        public bool JustReleased(params Keys[] oKeys)
        {
            foreach (Keys key in oKeys)
            {
                bool keyDown = _PrevState.IsKeyDown(key);
                bool keyUp = _CurrState.IsKeyUp(key);

                if (keyDown && keyUp)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Dertermines if a key is currently being pressed
        /// </summary>
        /// <param name="oKeys">Keys to Check</param>
        /// <returns>True if key is currently pressed</returns>
        public bool IsPressedOne(params Keys[] oKeys)
        {
            foreach (Keys key in oKeys)
            {
                bool keyDown = _CurrState.IsKeyDown(key);

                if (keyDown == true)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Dertermines if a key is currently being pressed
        /// </summary>
        /// <param name="oKeys">Keys to Check</param>
        /// <returns>True if key is currently pressed</returns>
        public bool IsPressedAll(params Keys[] oKeys)
        {
            foreach (Keys key in oKeys)
            {
                bool keyDown = _CurrState.IsKeyDown(key);

                if (keyDown == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Dertermines if a key is currently being released
        /// </summary>
        /// <param name="oKeys">Keys to Check</param>
        /// <returns>True if key is currently released</returns>
        public bool IsReleased(params Keys[] oKeys)
        {
            foreach (Keys key in oKeys)
            {
                bool keyUp = _CurrState.IsKeyUp(key);

                if (keyUp == false)
                    return false;
            }

            return true;
        }

        #endregion

        KeyboardState _PrevState;
        KeyboardState _CurrState;

    }
}
